/*
Copyright (C) 2015 Marien Raat

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <mainGame.h>
#include <iostream>
#include <math.h>

MainGame::MainGame(sf::RenderWindow *window, sf::Vector2u screenSize)
    : player(sf::Vector2f(screenSize.x, screenSize.y) / 2.0f, 0.0f,
             7.5f, screenSize),
      enemyManager(screenSize)
{
    this->window = window;
    this->screenSize = screenSize;
    dead = false;

    // Load textures
    playerTexture.loadFromFile("art/Amoeba.png");
    playerSprite.setTexture(playerTexture);
    scoreFont.loadFromFile("fonts/UbuntuMono-B.ttf");
    scoreText.setFont(scoreFont);
    scoreText.setCharacterSize(40);
    float pad = 10;
    scoreText.setPosition(pad, pad);
    scoreText.setColor(sf::Color::White);

    endText.setFont(scoreFont);
    endText.setCharacterSize(40);
    endText.setPosition(280, 200);
    endText.setColor(sf::Color::White);
}

void MainGame::start()
{
    while (window->isOpen() && !dead) {
        handleEvents();
        handleInput();
        update();
        draw();
    }
    if (player.getRadius() > 72) {
        endText.setString("You win!\n"
                          "You have taken over the\n"
                          "entire ecosystem and have\n"
                          "become the biggest amoeba\n"
                          "to ever have lived!\n\n"
                          "Press return to continue...");
    }
    else
        endText.setString("You got eaten.\nYou scored " +
                          std::to_string((int) pow(player.getRadius(), 2.0) - 56)
                          + "\n\nPress return to continue...");
    while (window->isOpen() &&
           !sf::Keyboard::isKeyPressed
           (sf::Keyboard::Return)) {
        handleEvents();
        window->clear(sf::Color(9, 44, 49));
        window->draw(endText);
        window->display();
    }
}

void MainGame::handleEvents()
{
    sf::Event event;
    while (window->pollEvent(event)) {
        switch (event.type) {
        case sf::Event::Closed:
            window->close();
            break;
        }
    }
}

void MainGame::handleInput()
{
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) ||
        sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        player.turnLeft();
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) ||
        sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        player.turnRight();
}

void MainGame::update()
{
    player.update();
    dead = dead || enemyManager.update(&player);
    dead = dead || (player.getRadius() > 72);
}

void MainGame::draw()
{
    window->clear(sf::Color(9, 44, 49));
    enemyManager.draw(window);
    drawPlayer();
    window->display();
}

void MainGame::drawPlayer()
{
    float scale = (2.5 * player.getRadius()) / 451.0;
    playerSprite.setScale(scale, scale);
    playerSprite.setOrigin(225.5, 225.5);
    playerSprite.setPosition(player.getPosition());
    playerSprite.setRotation(player.getRotation());
    window->draw(playerSprite);

    scoreText.setString(std::to_string
                        ((int) pow(player.getRadius(),
                                   2.0) - 56));
    window->draw(scoreText);
}
